const app = getApp();
import { index as design } from '../../../api/design';
import { index } from '../../../api/mine/index';
import { checkin } from '../../../api/mine/checkin';
import { getInfo } from '../../../utils/user';

Page({
  data: {
    height: 64, //header高度
    scrollHeight: 0, //滚动总高度
    top: 0, //标题图标距离顶部距离
    opcity: 0,
    iconOpcity: 0.5,
    //
    loading: true,
    info: null,
    header: null,
    design: []
  },
  onLoad(options) {
    this.setHeader()
    this.setData({ loading: true })
    design('mine').then(res => {
      const { data } = res;
      this.setData({
        header: data[1].data,
        design: data
      });
    })
      .finally(() => {
        this.setData({ loading: false });
      });
  },
  onShow() {
    if (app.login()) {
      index().then(res => {
        this.setData({
          points: res.data.points,
          label: res.data.label,
          info: getInfo()
        });
      });
    }
  },
  onPageScroll(event) {
    let scroll = event.scrollTop <= 0 ? 0 : event.scrollTop;
    let opcity = scroll / this.data.scrollHeight;
    if (this.data.opcity >= 1 && opcity >= 1) {
      return;
    }
    this.setData({
      opcity: opcity,
      iconOpcity: 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
    })
  },
  onPullDownRefresh() {/*监听用户下拉动作*/ },
  onReachBottom() {/*页面上拉触底事件的处理函数*/ },

  //===================method================//

  setHeader() {
    let menu = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: menu.left,
      height: menu.top + menu.height + 8,
      top: menu.top + (menu.height - 32) / 2
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          this.setData({
            scrollHeight: res.windowWidth
          })
        }
      })
    });
  },

  avatarClick() {
    if (!app.login()) {
      this.setData({
        login: true
      });
    }
  },

  redirect(event) {
    if (app.login()) {
      app.redirect(event.currentTarget.dataset.url);
    } else {
      this.setData({
        login: true
      });
    }
  },

  goBack() {
    wx.navigateBack({
      delta: 1
    });
  },

  checkin() {
    wx.showLoading({
      title: '签到中...'
    });
    checkin().then(res => {
      this.setData({
        animation: 'animation-scale-down',
        points: this.data.points + res.data,
        checkin: 1
      });
    })
      .finally(() => {
        wx.hideLoading();
      });
  }
})
